import java.sql.*;

import java.util.Scanner;




/**
 * @author hyhWTX
 * @version 1.0
 * @ClassName jdbc
 * @date 2022年08月11日 9:26
 * @Description: jdbc练习部分
 *
 */
public class jdbc {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
     //addUser();
        //del();
      update();
    }


//    @Test
//    public  void  Test01() throws SQLException {
//        Driver driver = new Driver();
//        String url = "jdbc:mysql://localhost:3306/jdbchyh";
//
//        //将用户名和密码放入到Properties对象
//        //将 用户名和密码放入到Properties 对象
//        Properties properties = new Properties();
//        properties.setProperty("user","root");//用户
//        properties.setProperty("password","root");//密码
//
//        //获取连接
//        Connection connect = driver.connect(url, properties);
//
//        System.out.println("方式1"+connect);
//}

    public  static void addUser() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc-db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
        Scanner input = new Scanner(System.in);
        System.out.print("请输入用户名:");
        String username = input.next();
        System.out.print("请输入密码:");
        String password = input.next();
        //创建SQL语句？号表示占位符
        String sql = "insert into tbl_user value (default ,?,?)";
        //预处理对象
        PreparedStatement ps = con.prepareStatement(sql);
        //设置?号占位符的值
        ps.setString(1,username);
        ps.setString(2,password);
        //发送并执行SQL语句,executeUpdate执行增、删、改,返回数据库中受影响的行数
        int num = ps.executeUpdate();
        if (num>0) {
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
        //释放数据库资源
        ps.close();
        con.close();


    }
    //删除数据库信息
    public static   void del() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc-db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");

        Scanner input = new Scanner(System.in);
        System.out.print("请输入用户编号:");
        int userId = input.nextInt();
        //创建SQL语句
        String sql = "delete from tbl_user where user_id=?";
        //创建预处理对象

        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,userId);
        //发送并执行SQL语句,executeUpdate执行增、删、改,返回数据库中受影响的行数
        int num = ps.executeUpdate();
        if(num>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        //释放数据库资源
        ps.close();
        conn.close();
    }
    //修改数据库数据信息
    public static void update() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc-db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root");
//        Scanner in = new Scanner(System.in);
//        System.out.println("请输入你要修改的数据对应的id：");
//        int i = in.nextInt();
//        //创建查询数据语句
//        String sql = "select user_id  from tbl_user where user_id=?";
//        //创建预处理对象
//        PreparedStatement pre = con.prepareStatement(sql);
//        pre.setString(1, String.valueOf(i));
//        pre.setInt(1,i);
//        //发送并执行SQL语句,executeUpdate执行增、删、改,返回数据库中受影响的行数
//       boolean num = pre.getMoreResults();
//        if (num==true){
//            System.out.println("该数据存在数据库中！");
//            Scanner inn = new Scanner(System.in);
//            System.out.print("请输入用户名:");
//            String username = inn.next();
//            System.out.print("请输入密码:");
//            String password = inn.next();
//            String sqlStr = "update from tbl_user value (default,?,?) ";
//            PreparedStatement ps = con.prepareStatement(sqlStr);
//            //设置?号占位符的值
//            ps.setString(1,username);
//            ps.setString(2,password);
//            System.out.println("修改成功！");
//        }else {
//            System.out.println("您输入的数据有误！");
//        }
//        //释放数据库资源
//        pre.close();
//        con.close();
        Scanner input = new Scanner(System.in);
        System.out.print("请输入用户编号:");
        int userId = input.nextInt();
        String str = "select user_id from tbl_user where user_id=?";
        //预处理对象
        PreparedStatement pre = con.prepareStatement(str);
        pre.setInt(1, userId);
        Statement st = con.createStatement();
        ResultSet set = st.executeQuery(str);
        if (set.next()){
            System.out.println("存在该数据！");
        }else {
            System.out.println("不存在该数据！");
        }


    }
}
